//
//  HomeOwnViewModel.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/3.
//

import Foundation
import SwiftUI

class ChatFunctionViewModel: BaseViewModel {
    
    // 枚举用于标识分组类型
    enum GroupedItem: Identifiable {
        case title(ChatListTitleBean)
        case functions([ChatFunctionBean])
        
        var id: UUID {
            switch self {
            case .title(let bean):
                return bean.id
            case .functions(let beans):
                return beans.first?.id ?? UUID()
            }
        }
    }
    
    @Published var items = [GroupedItem]()
    @Published var status = ""
    
    func getData() {
        pageSize = 1
        var listAll: [Any] = []
        
        var listFunction: [ChatFunctionBean] = []
        listFunction.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_LETTER,
            title: "每周来信",
            description: "每周一 6点-12点，心灵朋友来信",
            resource: .homeIconMail,
            padding: 4
        ))
        listFunction.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_CALL,
            title: "来电记录",
            description: "每通来电全记录，沟通痕迹随时查",
            resource: .homeIconCall,
            padding: 2
        ))
        //        listFunction.append(ChatFunctionBean(
        //            title: "足迹",
        //            description: "地图可视化查看，每一步轨迹都清晰可寻"
        //        ))
        //        listFunction.append(ChatFunctionBean(
        //            title: "迁移",
        //            description: "仅支持本地迁移"
        //        ))
        listFunction.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_MEMORY,
            title: "记忆库",
            description: "不定时的会自动提取重要记忆",
            resource: .homeIconMemory,
            padding: 6
        ))
        
        listAll.append(ChatListTitleBean(
            title: "功能",
            description: "",
            count: listFunction.count
        ))
        listAll.append(contentsOf: listFunction)
        
        var listConfig: [ChatFunctionBean] = []
        listConfig.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_AI,
            title: "AI设置",
            description: "轻松切换 AI 功能",
            resource: .homeIconChat2,
            padding: 7
        ))
        listConfig.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_VOICE,
            title: "语音设置",
            description: "自定义个性铃声，随心挑选多样音色",
            resource: .homeIconVoice,
            padding: 4
        ))
        listConfig.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_CHAT,
            title: "聊天设置",
            description: "2种模型随意切换",
            resource: .myIconBubble,
            padding: 3
        ))
        listConfig.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_DIARY,
            title: "日记设置",
            description: "日记专属 AI 心灵配置",
            resource: .homeIconDiary,
            padding: 3
        ))
        //        listConfig.append(ChatFunctionBean(
        //            type: ChatFunctionType.TYPE_INSTRUCTION,
        //            title: "指令玩法",
        //            description: "唤醒心灵，解锁创意无限可能",
        //            resource: .homeIconInstruction,
        //            padding: 7
        //
        //        ))
        listConfig.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_AI_CUSTOM,
            title: "API连接",
            description: "支持多种先进的AI模型",
            resource: .homeIconApi,
            padding: 7
            
        ))
        listConfig.append(ChatFunctionBean(
            type: ChatFunctionType.TYPE_ACT,
            title: "活动",
            description: "每月每天都可以免费领取次数",
            resource: .homeIconGift,
            padding: 4
        ))
        listAll.append(ChatListTitleBean(
            title: "设置",
            description: "",
            count: listConfig.count
        ))
        listAll.append(contentsOf: listConfig)
        
        self.noMore = true
        self.items = groupItems(list: listAll)
    }
    
    // 预处理数据，将连续的同类型数据分组
    private func groupItems(list: [Any]) -> [GroupedItem] {
        var result: [GroupedItem] = []
        var tempFunctions: [ChatFunctionBean] = []
        
        for item in list {
            if let function = item as? ChatFunctionBean {
                tempFunctions.append(function)
            } else if let title = item as? ChatListTitleBean {
                if !tempFunctions.isEmpty {
                    result.append(.functions(tempFunctions))
                    tempFunctions = []
                }
                result.append(.title(title))
            }
        }
        
        if !tempFunctions.isEmpty {
            result.append(.functions(tempFunctions))
        }
        return result
    }
    
    func reset() {
        isInit = false
        items.removeAll()
    }
}
